package com.example.dicos.service.impl;

import com.aliyun.tea.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dicos.entity.ManHour;
import com.example.dicos.entity.WxUser;
import com.example.dicos.entity.dot.ManHourDto;
import com.example.dicos.entity.dot.ManHourUpdateDto;
import com.example.dicos.entity.vo.HostVo;
import com.example.dicos.mapper.ManHourMapper;
import com.example.dicos.service.ManHourService;
import com.example.dicos.service.WxUserService;
import com.example.dicos.util.TimeStamp;
import com.example.dicos.util.TimeStampUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: 朱超
 * @createDate: 2022/7/15
 * @version: 1.0
 * @remark：备注
 */
@Service
public class ManHourServiceImpl implements ManHourService {
    @Autowired
    private ManHourMapper manHourMapper;

    @Autowired
    private WxUserService wxUserService;

    /**
     * 列表
     *
     * @return
     */
    @Override
    public List<ManHour> queryManHourList() {
        //创建对象
        QueryWrapper<ManHour> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("creation_time");
        return manHourMapper.selectList(wrapper);
    }

    /**
     * 根据用户名称 查询当月数据
     *
     * @param userName
     * @return
     */
    @Override
    public List<ManHour> queryByName(String userName,String date){
        QueryWrapper<ManHour> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName).like("creation_time",date);
        if (StringUtils.isEmpty(date)){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
            String format = sdf.format(new Date());
            queryWrapper.eq("user_name",userName).like("creation_time",format);
            //添加排序条件
            queryWrapper.orderByDesc("creation_time");
        }
        return manHourMapper.selectList(queryWrapper);
    }

    /**
     * 根据用户名称和选择日期查询某月信息
     * @param userName
     * @param date
     * @return
     * @throws ParseException
     */
    @Override
    public List<ManHour> queryDate(String date){
        QueryWrapper<ManHour> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("creation_time",date);
        queryWrapper.orderByDesc("creation_time");
        return manHourMapper.selectList(queryWrapper);
    }

    @Override
    public List<ManHour> queryByNameList(String userName){
        QueryWrapper<ManHour> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        queryWrapper.orderByDesc("creation_time");
        return manHourMapper.selectList(queryWrapper);
    }

    /**
     * 添加信息并计算工时
     *
     * @param manHour
     * @return
     */
    @Override
    public Integer addManHour(ManHourDto manHour) throws ParseException {
        ManHour man = new ManHour();
        WxUser user = wxUserService.queryById(manHour.getUserId());
        manHour.setCreationTime(new Date());
        manHour.setUpdateTime(new Date());
        manHour.setUserId(user.getId());//用户id
        manHour.setUserName(user.getUserName());//用户姓名
        long lastedTime = TimeStamp.calLastedTime(TimeStampUtils.dateForString(manHour.getStartTime()), TimeStampUtils.dateForString(manHour.getEndTime()));
        Integer timeout = manHour.getTimeout();
        if (timeout == 0) {
            Long minute = TimeStamp.minute();
            Long time = (lastedTime - minute);
            String stamp = TimeStamp.timeStampToString(time);
            String stamps = TimeStamp.timeStampToNumber(time);
            BigDecimal decimal = this.bigDecimal(stamps);
            manHour.setHost(decimal);
            manHour.setDuration(stamp); //工作时长
        } else {
            String stamp = TimeStamp.timeStampToString(lastedTime);
            String stamps = TimeStamp.timeStampToNumber(lastedTime);
            BigDecimal decimal = this.bigDecimal(stamps);
            manHour.setHost(decimal);
            manHour.setDuration(stamp);
        }
        BeanUtils.copyProperties(manHour, man);
        return manHourMapper.insert(man);
    }

    /**
     * 修改信息
     *
     * @param manHourUpdate
     * @return
     */
    @Override
    public Integer updateManHour(ManHourUpdateDto manHourUpdate) throws ParseException {
        ManHour man = new ManHour();
        manHourUpdate.setUpdateTime(new Date());
        long lastedTime = TimeStamp.calLastedTime(TimeStampUtils.dateForString(manHourUpdate.getStartTime()), TimeStampUtils.dateForString(manHourUpdate.getEndTime()));
        Integer timeout = manHourUpdate.getTimeout();
        if (timeout == 0) {
            Long minute = TimeStamp.minute();
            Long time = (lastedTime - minute);
            String stamp = TimeStamp.timeStampToString(time);
            String stamps = TimeStamp.timeStampToNumber(time);
            BigDecimal decimal = this.bigDecimal(stamps);
            manHourUpdate.setHost(decimal);
            manHourUpdate.setDuration(stamp); //工作时长
        } else {
            String stamp = TimeStamp.timeStampToString(lastedTime);
            String stamps = TimeStamp.timeStampToNumber(lastedTime);
            BigDecimal decimal = this.bigDecimal(stamps);
            manHourUpdate.setHost(decimal);
            manHourUpdate.setDuration(stamp);
        }
        BeanUtils.copyProperties(manHourUpdate, man);
        return manHourMapper.updateById(man);
    }

    /**
     * 计算当前用户工时总和
     *
     * @param userId
     * @return
     */
    @Override
    public BigDecimal statisticsUserHour(Long userId,String date) {
        List<HostVo> integers = manHourMapper.statisticsUserHour(userId,date);
        BigDecimal result = integers.stream().map(HostVo::getHost).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal decimal = result.setScale(2, RoundingMode.HALF_UP);
        return decimal;
    }

    /**
     * 计算全部工时总和
     *
     * @return
     */
    @Override
    public BigDecimal statisticsHour(String date) {
        List<HostVo> hostVos = manHourMapper.statisticsHour(date);
        BigDecimal result = hostVos.stream().map(HostVo::getHost).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal decimal = result.setScale(2, RoundingMode.HALF_UP);
        return decimal;
    }


    protected BigDecimal bigDecimal(String stamps) {
        BigDecimal orderPrice = new BigDecimal(Double.valueOf(stamps));
        BigDecimal decimal = orderPrice.setScale(2, RoundingMode.HALF_UP);
        return decimal;
    }
}
